/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.internal.prefs;

import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

import net.miginfocom.swing.MigLayout;

import org.kommando.core.controlcenter.PreferencePage;
import org.kommando.skin.Skin;
import org.kommando.skin.SkinManager;
import org.osgi.service.prefs.Preferences;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.swing.EventListModel;

/**
 * @author Peter De Bruycker
 * 
 */
public class SkinPreferencePage implements PreferencePage {

    private static final String SKIN_KEY = "skin";

    // TODO preview functionality

    private JPanel panel;

    private SkinManager skinManager;

    private JList skinList;

    private Preferences prefs;

    public SkinPreferencePage(SkinManager skinManager, Preferences prefs) {
        this.skinManager = skinManager;
        this.prefs = prefs;
    }

    @Override
    public boolean apply() {
        Skin skin = (Skin) skinList.getSelectedValue();
        if (skin != null) {
            skinManager.setCurrent(skin);
            prefs.put(SKIN_KEY, skin.getId());

            return true;
        }

        return false;
    }

    @Override
    public JComponent getComponent() {
        if (panel == null) {
            panel = new JPanel(new MigLayout());

            skinList = new JList();

            EventList<Skin> skins = new BasicEventList<Skin>();

            skins.addAll(skinManager.getSkins());
            ListModel listModel = new EventListModel<Skin>(skins);
            skinList.setModel(listModel);

            skinList.setCellRenderer(new DefaultListCellRenderer() {
                @Override
                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                        boolean cellHasFocus) {
                    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

                    Skin skin = (Skin) value;
                    setText(skin.getName());

                    return this;
                }
            });

            panel.add(new JScrollPane(skinList));
        }

        return panel;
    }

    @Override
    public String getName() {
        return "Skins";
    }

    @Override
    public void restoreDefaults() {
        Skin skin = getSkin("skin:bezel");
        skinList.setSelectedValue(skin, true);
    }

    private Skin getSkin(String id) {
        for (Skin s : skinManager.getSkins()) {
            if (s.getId().equals(id)) {
                return s;
            }
        }

        return null;
    }

    @Override
    public String getId() {
        return "skin";
    }

    @Override
    public String getParentId() {
        return null;
    }
}
